Imports Microsoft.VisualBasic
Imports Cdm.DataCore.UserSecurity

Public Class UserAuthorization

    Public Enum LoginStatus As Integer
        NotAuthenticated = 0
        Authenticated = 1
        Expired = 2
    End Enum
    ''' <summary>
    ''' Attempts to login user
    ''' </summary>
    ''' <param name="userName">The user's log in name</param>
    ''' <param name="password">The user's password</param>
    ''' <returns>An enum of type LoginStatus (NotAuthenticated, Expired, Authenticated)</returns>
    ''' <remarks>Sets the Session User Object upon success.</remarks>
    Public Shared Function LoginUser(ByVal userName As String, _
                                     ByVal password As String) As LoginStatus

        ' Attempt to retrieve the user by username and password.
        Dim newUser As Cdm.DataCore.UserSecurity.CDMUser = Cdm.DataCore.UserSecurity.CDMUser.Retrieve(userName, password)

        ' Determine if the user exists, their password has expired, or does not exist.
        If newUser Is Nothing Then

            Return LoginStatus.NotAuthenticated

        ElseIf newUser.PasswordExpires < Now Then

            System.Web.HttpContext.Current.Session("CurrentUser") = newUser
            System.Web.HttpContext.Current.Session("LoginId") = newUser.LogonId
            System.Web.HttpContext.Current.Session("CurrentUserId") = newUser.UserId

            Return LoginStatus.Expired

        Else

            If newUser.Status = "Inactive" Then
                Return LoginStatus.NotAuthenticated
            End If

            System.Web.HttpContext.Current.Session("CurrentUser") = newUser
            System.Web.HttpContext.Current.Session("LoginId") = newUser.LogonId
            System.Web.HttpContext.Current.Session("CurrentUserId") = newUser.UserId

            Return LoginStatus.Authenticated

        End If

    End Function
    ''' <summary>
    ''' Returns the CurrentUser Session Object.  This object is of Type CDMUser
    ''' and is set when a user logs in.
    ''' </summary>
    ''' <returns>The currently logged in user, in a CDMUser object</returns>
    ''' <remarks>If there is no user, page gets redirected to login.</remarks>
    Public Shared Function CurrentUser() As Cdm.DataCore.UserSecurity.CDMUser
        If System.Web.HttpContext.Current.Session("CurrentUser") Is Nothing Then
            System.Web.HttpContext.Current.Response.Redirect("~/login.aspx")
        End If
        Return System.Web.HttpContext.Current.Session("CurrentUser")
    End Function
    ''' <summary>
    ''' Checks if user has readonly permission
    ''' </summary>
    ''' <param name="permissionName">Permission Type to check for the current user</param>
    ''' <returns>True if user has the permission, but does not have insert or update permission, false otherwise</returns>
    ''' <remarks></remarks>
    Public Shared Function HasReadOnlyPermission(ByVal permissionName As Permission.PermissionType) As Boolean
        Dim currentPermission As IUserPermission = CurrentUser.Permissions(permissionName)
        If currentPermission Is Nothing Then
            Return False
        Else
            Return (Not (currentPermission.MayUpdate Or currentPermission.MayInsert)) And currentPermission.MayView
        End If
    End Function

    ''' <summary>
    ''' Checks if CurrentUsere has Update permission
    ''' </summary>
    ''' <param name="permissionName">Permission Type to Check for the current user</param>
    ''' <returns>true if user has update permission; false otherwise</returns>
    ''' <remarks></remarks>
    Public Shared Function HasUpdatePermission(ByVal permissionName As Permission.PermissionType) As Boolean
        Dim updatePermission As IUserPermission = CurrentUser.Permissions(permissionName)
        If updatePermission Is Nothing Then
            Return False
        Else
            Return updatePermission.MayUpdate
        End If
    End Function

    ''' <summary>
    ''' Checks if CurrentUsere has View permission
    ''' </summary>
    ''' <param name="permissionName">Permission Type to Check for the current user</param>
    ''' <returns>true if user has view permission; false otherwise</returns>
    ''' <remarks></remarks>
    Public Shared Function HasViewPermission(ByVal permissionName As Permission.PermissionType) As Boolean
        Dim updatePermission As IUserPermission = CurrentUser.Permissions(permissionName)
        If updatePermission Is Nothing Then
            Return False
        Else
            Return updatePermission.MayView
        End If
    End Function


    ''' <summary>
    ''' Checks if CurrentUser has Insert permission
    ''' </summary>
    ''' <param name="permissionName">Name of the Permission to Check for the current user</param>
    ''' <returns>true if user has insert permission; false otherwise</returns>
    ''' <remarks></remarks>
    Public Shared Function HasInsertPermission(ByVal permissionName As Permission.PermissionType) As Boolean
        Dim insertPermission As IUserPermission = CurrentUser.Permissions(permissionName)
        If insertPermission Is Nothing Then
            Return False
        Else
            Return insertPermission.MayInsert
        End If
    End Function

    Public Shared Sub CheckPagePermission(ByVal permissionName As Permission.PermissionType, ByVal permissionlevel As String)
        Dim hasPermission As Boolean
        Select Case UCase(permissionlevel)
            Case "INSERT"
                hasPermission = HasInsertPermission(permissionName)
            Case "UPDATE"
                hasPermission = HasUpdatePermission(permissionName)
            Case "VIEW"
                hasPermission = HasViewPermission(permissionName)
            Case "DELETE"
                hasPermission = HasDeletePermssion(permissionName)
        End Select

        If hasPermission = False Then
            System.Web.HttpContext.Current.Response.Redirect("~/default.aspx")
        End If
    End Sub

    Public Shared Function HasDeletePermssion(ByVal permissionName As Permission.PermissionType) As Boolean
        Dim deletePermission As IUserPermission = CurrentUser.Permissions(permissionName)
        If deletePermission Is Nothing Then
            Return False
        Else
            Return deletePermission.MayDelete
        End If
    End Function
End Class
